#!/bin/sh

# manpage tests using man/roff
#
# Contact: grubert@users.sourceforge.net
# Revision: $Revision$
# Date: $Date$
# Copyright: This script has been placed in the public domain.

IN_DIR=input
OUT_DIR=output
EXP_DIR=expected

if [ -z "`man -?|grep -- '^ *-T'`" ] ; then
    echo 'WARNING: your man does not support "-T Format" to specify encoding'
    echo 'SKIPPING encoding tests'
    ENCS=""
else
    ENCS="latin1 utf8 ascii ps"
fi

if [ -z "$1" ] ; then
  FILES=*.txt
else
  FILES=$1
fi

ERROR_CNT=0

inc_ERROR_CNT () {
    ERROR_CNT=$(( ERROR_CNT + 1 ))
}

report_ERROR_CNT () {
	echo "$ERROR_CNT errors"
}

CMD="../../docutils/tools/rst2man.py --traceback"

for IN_F in $IN_DIR/$FILES ; do
  BASE=`basename $IN_F`
  F_BASE=${BASE%.txt}
  if echo $F_BASE | grep -q -- '-de\.' ; then
    OPT="-l de"
  else
    OPT=""
  fi
  $CMD $OPT $IN_F $OUT_DIR/$F_BASE.man
  if [ $? -eq 0 ] ; then
    diff -s -u $EXP_DIR/$F_BASE.man $OUT_DIR/$F_BASE.man
    if [ $? -ne 0 ] ; then
      inc_ERROR_CNT
    fi
    for ENC in $ENCS ; do
      if [ -e $EXP_DIR/$F_BASE.$ENC ] ; then
        man -T$ENC $OUT_DIR/$F_BASE.man > $OUT_DIR/$F_BASE.$ENC
        diff -s -u $EXP_DIR/$F_BASE.$ENC $OUT_DIR/$F_BASE.$ENC
        if [ $? -ne 0 ] ; then
          inc_ERROR_CNT
        fi
      fi
    done
  fi
done

if [ -n "$1" ] ; then
    report_ERROR_CNT
    exit
fi

# language test
for L in "en" "de" ; do
  for IN_F in $IN_DIR/docinfo-*.txt ; do
    BASE=`basename $IN_F`
    F_BASE=${BASE%.txt}
    OPT="-l $L"
    EXT="-l_$L"
    $CMD $OPT $IN_F $OUT_DIR/$F_BASE$EXT.man
    if [ $? -eq 0 ] ; then
      diff -s -u $EXP_DIR/$F_BASE$EXT.man $OUT_DIR/$F_BASE$EXT.man
      if [ $? -ne 0 ] ; then
        inc_ERROR_CNT
      fi
    fi
  done
done

report_ERROR_CNT

